package com.ruoyi.basebill.task;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.basebill.domain.WfzWebservice;
import com.ruoyi.basebill.service.IWfzWebserviceService;
import com.ruoyi.basedata.domain.WfzBaseunit;
import com.ruoyi.basedata.service.IWfzBaseunitService;
import com.ruoyi.webServiceUtil.WSWfzHrztFacadeSrvProxy;
import com.ruoyi.webServiceUtil.WSWfzHrztFacadeSrvProxyServiceLocator;
import com.ruoyi.webServiceUtil.WebServiceHelper;
import org.apache.axis.client.Stub;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.xml.rpc.ServiceException;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import java.text.ParseException;
import java.util.List;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 读取eas的成本组织
 */
@Component("OrgTask")
public class OrgTask {

    @Autowired
    private IWfzWebserviceService wfzWebserviceService;

    @Autowired
    private IWfzBaseunitService wfzBaseunitService;

    public void fromEAS(){
        WfzWebservice wfzWebservice = new WfzWebservice();
        wfzWebservice.setJknumber("EAS001");
        List<WfzWebservice> wfzWebservices = wfzWebserviceService.selectWfzWebserviceList(wfzWebservice);
        wfzWebservice = wfzWebservices.get(0);
        String jkpassword = wfzWebservice.getJkpassword(); //密码
        String jkinurl = wfzWebservice.getJkinurl(); //业务接口
        String dcname = wfzWebservice.getDcname(); //数据中心名称
        String jkurl = wfzWebservice.getJkurl(); //登录接口
        String jkmethod = wfzWebservice.getJkmethod(); //业务方法
        String jkusername = wfzWebservice.getJkusername(); //接口用户

        try {
            String login = WebServiceHelper.login(jkusername, jkpassword, jkurl, dcname);
//                String result = WebServiceHelper.serviceInterface(login, jkinurl, jkmethod, json, "WFZ002");
            URL url = new URL(jkinurl);
            WSWfzHrztFacadeSrvProxy wsWfzHrztFacade = new WSWfzHrztFacadeSrvProxyServiceLocator().getWSWfzHrztFacade(url);
            ((Stub) wsWfzHrztFacade).setHeader("http://login.webservice.bos.kingdee.com","SessionId",login);
            String result = wsWfzHrztFacade.getOrg("2023");
            JSONArray jsonArray = JSONArray.parseArray(result);
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject reInfo = JSONObject.parseObject(jsonArray.getString(i));
                String lastUpdateTime = reInfo.getString("lastUpdateTime");//最后修改时间
                try {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    Date date = sdf.parse(lastUpdateTime);
                    // 获取String字符串中的年
                    SimpleDateFormat y = new SimpleDateFormat("yyyy");
                    // 获取String字符串中的月
                    SimpleDateFormat m = new SimpleDateFormat("MM");
                    // 获取String字符串中的日
                    SimpleDateFormat d = new SimpleDateFormat("dd");
                    if (y.format(date).equals(y.format(new Date())) &&
                        m.format(date).equals(m.format(new Date())) &&
                        d.format(date).equals(d.format(new Date()))){
                        WfzBaseunit wfzQueryBaseunit = new WfzBaseunit();
                        wfzQueryBaseunit.setFnumber(reInfo.getString("number"));
                        List<WfzBaseunit> wfzBaseunits = wfzBaseunitService.selectWfzBaseunitList(wfzQueryBaseunit);
                        if(wfzBaseunits.size()>0){
                            System.out.println(reInfo.getString("number"));
                            WfzBaseunit wfzBaseunit = new WfzBaseunit();
                            String number = reInfo.getString("number");//组织编码
                            String name = reInfo.getString("name");//组织名称
                            String isBizCost = reInfo.getString("isBizCost");//是否实体成本中心
                            String isBizCompany = reInfo.getString("isBizCompany");//是否实体财务组织
                            String isOUSealUp = reInfo.getString("isOUSealUp");//是否封存
                            String level = reInfo.getString("level");//组织层级
                            String isLeaf = reInfo.getString("isLeaf");//是否叶子节点
                            wfzBaseunit.setFnumber(number);
                            wfzBaseunit.setFname(name);
                            wfzBaseunit.setFisbizcost(Integer.valueOf(isBizCost));
                            wfzBaseunit.setFisbizcompany(Integer.valueOf(isBizCompany));
                            wfzBaseunit.setFisousealup(Integer.valueOf(isOUSealUp));
                            wfzBaseunit.setFlevel(Long.valueOf(level));
                            wfzBaseunit.setFisleaf(Integer.valueOf(isLeaf));
                            wfzBaseunit.setFcreator("admin");
                            wfzBaseunit.setFcreatetime(date);
                            wfzBaseunit.setFlastupdateuser("admin");
                            wfzBaseunit.setFlastupdatetime(date);
                            int i1 = wfzBaseunitService.updateWfzBaseunit(wfzBaseunit);
                            System.out.println(i1);
                        }else{
                            System.out.println(reInfo.getString("number"));
                            WfzBaseunit wfzBaseunit = new WfzBaseunit();
                            String number = reInfo.getString("number");//组织编码
                            String name = reInfo.getString("name");//组织名称
                            String isBizCost = reInfo.getString("isBizCost");//是否实体成本中心
                            String isBizCompany = reInfo.getString("isBizCompany");//是否实体财务组织
                            String isOUSealUp = reInfo.getString("isOUSealUp");//是否封存
                            String level = reInfo.getString("level");//组织层级
                            String isLeaf = reInfo.getString("isLeaf");//是否叶子节点
                            wfzBaseunit.setFnumber(number);
                            wfzBaseunit.setFname(name);
                            wfzBaseunit.setFisbizcost(Integer.valueOf(isBizCost));
                            wfzBaseunit.setFisbizcompany(Integer.valueOf(isBizCompany));
                            wfzBaseunit.setFisousealup(Integer.valueOf(isOUSealUp));
                            wfzBaseunit.setFlevel(Long.valueOf(level));
                            wfzBaseunit.setFisleaf(Integer.valueOf(isLeaf));
                            wfzBaseunit.setFcreator("admin");
                            wfzBaseunit.setFcreatetime(date);
                            wfzBaseunit.setFlastupdateuser("admin");
                            wfzBaseunit.setFlastupdatetime(date);
                            int i1 = wfzBaseunitService.insertWfzBaseunit(wfzBaseunit);
                            System.out.println(i1);
                        }

                    }else{
                        continue;
                    }
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        } catch (ServiceException e) {
            throw new RuntimeException(e);
        } catch (RemoteException e) {
            throw new RuntimeException(e);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }

    }

    public static void main(String[] args) {
        Calendar now = Calendar.getInstance();
        System.out.println(now.get(Calendar.YEAR)+","+(now.get(Calendar.MONTH) + 1)+","+now.get(Calendar.DAY_OF_MONTH));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse("2022-1-9");
            SimpleDateFormat y = new SimpleDateFormat("yyyy");
            // 获取String字符串中的月
            SimpleDateFormat m = new SimpleDateFormat("MM");
            // 获取String字符串中的日
            SimpleDateFormat d = new SimpleDateFormat("dd");
            System.out.println( y.format(date)+","+ m.format(date)+","+ d.format(date));
            System.out.println( y.format(new Date())+","+ m.format(new Date())+","+ d.format(new Date()));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

    }

}
